package com.meet.service;

import com.meet.dao.WorkersDao;
import com.meet.dao.WorkersDaoImpl;
import com.meet.entity.PageBean;
import com.meet.entity.Workers;
import com.meet.vo.MeetingVo;
import com.meet.vo.WorkersVo;

import java.sql.SQLException;
import java.util.List;

/**
 * @Author LiDianyang
 * @Date 2022/8/11 19:42
 * @PackageName:com.meet.service
 * @ClassName: WorkersServiceImpl
 * @Description: TODO
 * @Version 1.0
 */
public class WorkersServiceImpl implements  WorkersService{

    private WorkersDao workersDao = new WorkersDaoImpl();

    public WorkersServiceImpl(WorkersDao workersDao) {
        this.workersDao = workersDao;
    }

    @Override
    public int addWorkers(Workers workers) throws SQLException {
        return workersDao.addWorkers(workers);
    }

    @Override
    public int updateWorkers(Workers workers) throws SQLException {
        return workersDao.updateWorkers(workers);
    }

    @Override
    public int deleteWorkers(Integer id) throws SQLException {
        return workersDao.deleteWorkers(id);
    }

    @Override
    public int getCount() throws SQLException {
        return workersDao.getCount();
    }

    @Override
    public List<WorkersVo> getWorkersCard(int page, int number) throws SQLException {
        return workersDao.getWorkersCard(page,number);
    }

    @Override
    public Workers getWorkers(String name, String password) throws SQLException {

        return workersDao.getWorkers(name, password);
    }

    @Override
    public Workers getWorkers(Integer id) throws SQLException {
        return workersDao.getWorkers(id);
    }

    @Override
    public PageBean<WorkersVo> getAllWorkersByPage(String _currentPage, String _rows) throws SQLException {
        int currentPage = Integer.parseInt(_currentPage);
        int rows = Integer.parseInt(_rows);

        if(currentPage <=0) {
            currentPage = 1;
        }

        //1.创建空的PageBean对象
        PageBean<WorkersVo> pb = new PageBean<WorkersVo>();
        //2.设置参数
        pb.setCurrentPage(currentPage);
        pb.setRows(rows);

        //3.调用dao查询总记录数
        int totalCount = workersDao.getCount();
        pb.setTotalCount(totalCount);

        //4.调用dao查询List集合
        //计算开始的记录索引
        int start = (currentPage - 1) * rows;
        List<WorkersVo> list = workersDao.getWorkersCard(start,rows);
        pb.setList(list);

        //5.计算总页码
        int totalPage = (totalCount % rows)  == 0 ? totalCount/rows : (totalCount/rows) + 1;
        pb.setTotalPage(totalPage);


        return pb;
    }

    @Override
    public List<Workers> getAllWorkers() throws SQLException {
        return workersDao.getAllWorkers();
    }

    @Override
    public WorkersVo getWorkersVo(Integer id) throws SQLException {
        return workersDao.getWorkersVo(id);
    }

    @Override
    public int updateWorkersphone(Workers workers) throws SQLException {
        return workersDao.updateWorkersphone(workers);
    }
}
